package ncme.auto.framework.core.logCore.service.impl;

import ncme.auto.framework.core.entities.aspect.AspectInfo;
import ncme.auto.framework.core.logCore.entity.LogManage;
import ncme.auto.framework.core.logCore.entity.LogManageSimple;
import ncme.auto.framework.core.logCore.entity.RequestLog;
import ncme.auto.framework.core.logCore.mapper.LogManageMapper;
import ncme.auto.framework.core.logCore.service.LogService;
import ncme.auto.framework.core.logCore.util.RequestUtil;
import ncme.auto.framework.core.services.Impl.AspectPermissionServiceImpl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Map;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private AspectPermissionServiceImpl userManagerService;
    @Autowired
    private LogManageMapper logManageMapper;
    @Autowired
    private LogManageSimpleServiceImpl logManageService;

    private static final Logger log = LoggerFactory.getLogger(LogService.class);

    public boolean saveLogToDB(LogManage logManage) {
        log.info(logManage.toString());
        LogManageSimple logManageSimple = new LogManageSimple();
        logManageSimple.setMsg(logManage.getMsg());
        logManageSimple.setOptType(logManage.getOptType());
        logManageSimple.setOptPersonId(logManage.getOptPersonId());
        logManageSimple.setErrorMsg(logManage.getErrorMsg());
        logManageSimple.setOptIp(logManage.getOptIp());
        logManageSimple.setExcTime(logManage.getExcTime());
        logManageSimple.setStatus(logManage.getStatus());
        logManageSimple.setRequestUrl(logManage.getRequestUrl());
        logManageSimple.setRequestType(logManage.getRequestType());
        logManageSimple.setRequestPlatform(logManage.getRequestPlatform());
        logManageSimple.setRequestBrowser(logManage.getRequestBrowser());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        logManageSimple.setCreateDate(dateFormat.format(logManage.getCreateDate()));
        logManageService.baseQueryAdd(logManageSimple);
        return true;
    }

    public boolean saveLog(ProceedingJoinPoint pjp, AspectInfo aspectInfo, Object result) {
        HttpServletRequest request = aspectInfo.getRequestInfo().getRequest();
        long requestTime = aspectInfo.getRequestInfo().getRequestTime();
        String logMsg = aspectInfo.getAnnotationInfo().getLOG_MSG();
        String requestMethod = pjp.getTarget().getClass().getName() + '.' + pjp.getSignature().getName();
        RequestLog requestLog = RequestUtil.getRequestInfo(request, requestMethod, pjp.getArgs(), result, requestTime);
        LogManage logManage = new LogManage();
        switch (request.getMethod()) {
            case "GET":
                logManage.setOptType("Select");
                break;
            case "POST":
                logManage.setOptType("Insert");
                break;
            case "DELETE":
                logManage.setOptType("Delete");
                break;
            case "PUT":
                logManage.setOptType("Update");
                break;
        }
        logManage.setCreateDate(requestLog.getRequestTime());
        logManage.setOptIp(requestLog.getHostIP());
        if (logMsg.equals("用户登录")) {
            Map parameterMap = (Map) pjp.getArgs()[0];
            if (parameterMap == null || parameterMap.get("userName") == null) logManage.setOptPersonId("无");
            else logManage.setOptPersonId(parameterMap.get("userName").toString());
        } else logManage.setOptPersonId(userManagerService.getUserNameFromRequest(request));
        logManage.setExcTime(requestLog.getExecuteTime());
        logManage.setErrorMsg(requestLog.getErrorMsg());
        logManage.setRequestParam(requestLog.getRequestParam());
        logManage.setResponseData(requestLog.getResponseParam());
        logManage.setStatus(requestLog.getResponseStatus());
        logManage.setRequestUrl(requestLog.getRequestURL());
        logManage.setRequestType(requestLog.getRequestType());
        logManage.setRequestMethodName(requestLog.getRequestMethodName());
        logManage.setRequestPlatform(requestLog.getRequestPlatform());
        logManage.setRequestBrowser(requestLog.getRequestBrowser());
        logManage.setMsg(logMsg.equals("") ? "" : getOptType(request) + logMsg);
        logManage.setErrorMsg("");

        return saveLogToDB(logManage);
    }

    protected String getOptType(HttpServletRequest request) {
        String opt = "";
        switch (request.getMethod()) {
            case "GET":
                opt = "获取";
                break;
            case "POST":
                opt = "创建";
                break;
            case "PUT":
                opt = "修改";
                break;
            case "DELETE":
                opt = "删除";
                break;
        }
        return opt;
    }

}
